package com.sudichina.sudichina.utils;

import android.app.Activity;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.sudichina.sudichina.MyApp;
import com.sudichina.sudichina.R;
import com.sudichina.sudichina.bean.CityPrice;

/**
 * Name: ChooseTypeDialog
 * Action:
 * Author: liuan
 * creatTime:2017-03-29 10:33
 */

public class ChooseTypeDialog {

    //斤方吨用到的变量
    private static final int DIALOG_JIN = 1001;
    private static final int DIALOG_FANG = 1002;
    private static final int DIALOG_DUN = 1003;
    private static final int DIALOG_ALLCAE = 1004;
    private static int DIALOG_CURRENTPOSITION = DIALOG_JIN;
    private static String STR_DIALOG_JIN = "";
    private static String STR_DIALOG_FANG = "";
    private static String STR_DIALOG_DUN = "";
    private static String STR_DIALOG_ALLCAR = "";
    private static String STR_CURRENT = "";


    private static TextView tvJin;
    private static TextView tvFang;
    private static TextView tvDun;
    private static EditText etPrice;
    private static boolean clearAvidListern = false;
    private static AlertDialog chooseWeightDialog;
    private static TextView tvAllcar;
    private static OnPiceChooseListener onPiceChooseListener;
    private static String mycityNames;

    public static void getType(Activity activity, String mycityName) {
        mycityNames = mycityName;
        showMyDialog(activity);


    }

    private static void showMyDialog(Activity activity) {

        chooseWeightDialog = new AlertDialog.Builder(activity).create();
        View view = View.inflate(MyApp.getContext(), R.layout.dialog_route, null);
        TextView tvXzCity = (TextView) view.findViewById(R.id.tv_xz_city);
        tvJin = (TextView) view.findViewById(R.id.tv_jin);
        tvFang = (TextView) view.findViewById(R.id.tv_fang);
        tvDun = (TextView) view.findViewById(R.id.tv_dun);
        tvAllcar = (TextView) view.findViewById(R.id.tv_allcar);
        etPrice = (EditText) view.findViewById(R.id.et_price);
        TextView cancel = (TextView) view.findViewById(R.id.tv_cancel);
        TextView tvConfrim = (TextView) view.findViewById(R.id.tv_confirm);
        //城市输入上
//        Toastutils.showToast("斤,方,吨,三个单价必须全部输入");
        tvXzCity.setText("请选择类型");
        tvJin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //清空的时候免除监听
                clearAvidListern = true;
                etPrice.setText("");
                //设置提示
                etPrice.setHint("请输入斤价格");
                clearAvidListern = false;

                DIALOG_CURRENTPOSITION = DIALOG_JIN;
                tvJin.setTextColor(Color.parseColor("#ffffff"));
                tvJin.setBackgroundColor(Color.parseColor("#439198"));
                tvFang.setTextColor(Color.parseColor("#000000"));
                tvFang.setBackgroundColor(Color.parseColor("#ffffff"));
                tvDun.setTextColor(Color.parseColor("#000000"));
                tvDun.setBackgroundColor(Color.parseColor("#ffffff"));
                tvAllcar.setTextColor(Color.parseColor("#000000"));
                tvAllcar.setBackgroundColor(Color.parseColor("#ffffff"));


            }
        });
        tvFang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clearAvidListern = true;
                etPrice.setText("");
                //设置提示
                etPrice.setHint("请输入方价格");
                clearAvidListern = false;
                DIALOG_CURRENTPOSITION = DIALOG_FANG;

                tvFang.setTextColor(Color.parseColor("#ffffff"));
                tvFang.setBackgroundColor(Color.parseColor("#439198"));
                tvJin.setTextColor(Color.parseColor("#000000"));
                tvJin.setBackgroundColor(Color.parseColor("#ffffff"));
                tvDun.setTextColor(Color.parseColor("#000000"));
                tvDun.setBackgroundColor(Color.parseColor("#ffffff"));
                tvAllcar.setTextColor(Color.parseColor("#000000"));
                tvAllcar.setBackgroundColor(Color.parseColor("#ffffff"));

            }
        });

        tvDun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clearAvidListern = true;
                etPrice.setText("");
                //设置提示
                etPrice.setHint("请输入吨价格");
                clearAvidListern = false;
                DIALOG_CURRENTPOSITION = DIALOG_DUN;
                tvDun.setTextColor(Color.parseColor("#ffffff"));
                tvDun.setBackgroundColor(Color.parseColor("#439198"));
                tvFang.setTextColor(Color.parseColor("#000000"));
                tvFang.setBackgroundColor(Color.parseColor("#ffffff"));
                tvJin.setTextColor(Color.parseColor("#000000"));
                tvJin.setBackgroundColor(Color.parseColor("#ffffff"));
                tvAllcar.setTextColor(Color.parseColor("#000000"));
                tvAllcar.setBackgroundColor(Color.parseColor("#ffffff"));
            }
        });
        tvAllcar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clearAvidListern = true;
                etPrice.setText("");
                //设置提示
                etPrice.setHint("请输入整车价格");
                clearAvidListern = false;
                DIALOG_CURRENTPOSITION = DIALOG_ALLCAE;
                tvAllcar.setTextColor(Color.parseColor("#ffffff"));
                tvAllcar.setBackgroundColor(Color.parseColor("#439198"));
                tvFang.setTextColor(Color.parseColor("#000000"));
                tvFang.setBackgroundColor(Color.parseColor("#ffffff"));
                tvJin.setTextColor(Color.parseColor("#000000"));
                tvJin.setBackgroundColor(Color.parseColor("#ffffff"));
                tvDun.setTextColor(Color.parseColor("#000000"));
                tvDun.setBackgroundColor(Color.parseColor("#ffffff"));
            }
        });
        tvConfrim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CityPrice cityPrice = new CityPrice();
                cityPrice.setName(mycityNames);
                cityPrice.setJinPrice(STR_DIALOG_JIN);
                cityPrice.setFangPrice(STR_DIALOG_FANG);
                cityPrice.setDunPrice(STR_DIALOG_DUN);
                cityPrice.setAllPrice(STR_DIALOG_ALLCAR);


                onPiceChooseListener.onPriceChoose(cityPrice);
                DIALOG_CURRENTPOSITION = DIALOG_JIN;

                chooseWeightDialog.dismiss();

            }
        });

        etPrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (clearAvidListern) {

                } else {
                    switch (DIALOG_CURRENTPOSITION) {
                        case DIALOG_JIN:
                            STR_DIALOG_JIN = s.toString();
                            break;
                        case DIALOG_FANG:
                            STR_DIALOG_FANG = s.toString();
                            break;
                        case DIALOG_DUN:
                            STR_DIALOG_DUN = s.toString();
                        case DIALOG_ALLCAE:
                            STR_DIALOG_ALLCAR = s.toString();
                            break;
                    }


                }

            }
        });
/*
*
*     chooseWeight.getWindow().setContentView(layout);
    chooseWeight.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    chooseWeight.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
            WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
* */
        chooseWeightDialog.show();
        Window window = chooseWeightDialog.getWindow();
        WindowManager.LayoutParams layoutparams = new WindowManager.LayoutParams();
        layoutparams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutparams.width = (int) MyApp.getContext().getResources().getDimension(R.dimen.x720);
        window.setAttributes(layoutparams);
        window.setContentView(view);
//        chooseWeight.setContentView(view);
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);


    }


    public static void setOnCityClickListener(OnPiceChooseListener listener) {
        onPiceChooseListener = listener;
    }

    public interface OnPiceChooseListener {
        void onPriceChoose(CityPrice cityPrice);

    }


}
